package com.service.lx;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dao.lx.DetectionDAO;
import com.dao.lx.RepairorderDAO;
import com.dao.lx.UserDAO;
import com.entity.Detection;
import com.entity.Repairorder;
import com.entity.User;
import com.service.ServiceException;


@Service
@Transactional(rollbackFor = Exception.class)
public class DetectionService {
	@Autowired
	private DetectionDAO detectionDAO;
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private RepairorderDAO repairorderDAO;
	/**
	 * 添加/修改/逻辑删除
	 * @param process
	 * @throws ServiceException
	 */
	public void addAndUpdateDetection(Detection detection) throws ServiceException{
		try {
			//判断用户是否存在
			if(detection.getUserId()!=null){
				User user=userDAO.findOne(detection.getUserId());
				if(user!=null){
					detection.setUser(user);
				}else{
					detection.setUser(null);
				}
			}
			//判断维修工单是否存在
			if(detection.getRepairorderId()!=null){
				Repairorder repairorder=repairorderDAO.findOne(detection.getRepairorderId());
				if(repairorder!=null){
					Detection detection3=detectionDAO.findByRepairorder(repairorder);
					if(detection3!=null){
						detection.setDetectionId(detection3.getDetectionId());
					}
					detection.setRepairorder(repairorder);
				}else{
					detection.setRepairorder(null);
				}
			}
			Detection detection2=detectionDAO.findOne(detection.getDetectionId());
			if(detection2==null){
				detection.setDetectionIfdelete(new BigDecimal(1));
			}
			detection.setDetectionIntodate(new Date());
			detectionDAO.save(detection);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 单条查询
	 * @param detectionId
	 * @return
	 */
	public Detection findOne(Long detectionId) {
		return detectionDAO.findOne(detectionId);
	}

}
